﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Sale_Operation;
using Sale_Common;
using SaleSystem;
using Sale_Model;

namespace SaleSystem.Stock
{
    public partial class frm_StockDetailList : PageBase
    {
        #region PageEvent
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                tb_SDeta_PInfo_Id.OnClientTrigger2Click = win_Content.GetSaveStateReference(tb_SDeta_PInfo_Id.ClientID, this.hf_SDeta_PInfo_Id.ClientID, this.hf_Other.ClientID, this.hf_Other.ClientID, this.hf_Other.ClientID, this.hf_Other.ClientID)
                         + win_Content.GetShowReference("~/ComPage/frm_ProductChooseList.aspx?PInfo_Id="); 

                BindDropDownList();
                GetAndBind();
            }
            else if (Request[LinkService.RefreshTag] == LinkService.RefreshString)
            {
                GetAndBind();
            }
        }
        protected void bnSearch_Click(object sender, EventArgs e)
        {
            GetAndBind();
        }
        
        protected void gv_List_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "edit")
            {
                win_Content2.Hidden = false;
                win_Content2.IFrameUrl = "../BaseData/frm_ProductInfoEdit.aspx?Id=" + ValueHandler.GetIntNumberValue(gv_List.DataKeys[e.RowIndex][0]);
                win_Content2.Title = "编辑-" + ValueHandler.GetStringValue(gv_List.DataKeys[e.RowIndex][1]);

            }
            else if (e.CommandName.ToLower() == "sdeta_outputnumber")
            {
                win_Content3.Hidden = false;
                win_Content3.IFrameUrl = "../ComPage/frm_BillList.aspx?Id=" + ValueHandler.GetIntNumberValue(gv_List.DataKeys[e.RowIndex][0])+"&Type=1";
                win_Content3.Title = "销售明细-" + ValueHandler.GetStringValue(gv_List.DataKeys[e.RowIndex][1]);
            }
            else if (e.CommandName.ToLower() == "sdeta_inputnumber")
            {

                win_Content3.Hidden = false;
                win_Content3.IFrameUrl = "../ComPage/frm_BillList.aspx?Id=" + ValueHandler.GetIntNumberValue(gv_List.DataKeys[e.RowIndex][0]) + "&Type=2";
                win_Content3.Title = "进货明细-" + ValueHandler.GetStringValue(gv_List.DataKeys[e.RowIndex][1]);
            }
        }
        protected void tb_SDeta_PInfo_Id_TriggerClick(object sender, EventArgs e)
        { 
            this.hf_Other.Text = "";
            this.tb_SDeta_PInfo_Id.Text = "";
        }
        protected void gv_List_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            gv_List.PageIndex = e.NewPageIndex;
            GetAndBind();
        }
        protected void gv_List_PreRowDataBound(object sender, ExtAspNet.GridPreRowEventArgs e)
        {
            ExtAspNet.LinkButtonField lbfAction1 = gv_List.FindColumn("edit") as ExtAspNet.LinkButtonField;
            ExtAspNet.LinkButtonField lbfAction2 = gv_List.FindColumn("delete") as ExtAspNet.LinkButtonField;
            DataRowView row = e.DataItem as DataRowView;
            if (ValueHandler.GetIntNumberValue(row["BBill_Input"]) == 1)
            {
                //                lbfAction1.Enabled = false;
                lbfAction1.Text = "查看";
                lbfAction2.Enabled = false;

            }
            else
            {
                lbfAction1.Enabled = true;
                lbfAction2.Enabled = true;
            }


        }
        #endregion

        #region Method

        private void GetAndBind()
        {
            FormatGrid(gv_List);
            InitCondition();
            dtList = Manager.GetList(strCondition, gv_List.PageIndex + 1, ref iRecordCount);
            gv_List.DataSource = dtList;
            gv_List.DataBind();
            gv_List.RecordCount = iRecordCount;
        }
        /// <summary>
        /// 获取查询条件
        /// </summary>
        public override void InitCondition()
        {
            strCondition = "";
            if (ddl_SDeta_Stock_Id.SelectedValue != "-1")
            {
                strCondition += "SDeta_Stock_Id="+this.ddl_SDeta_Stock_Id.SelectedValue+" AND ";
            }
            if (this.ddl_PInfo_PSort_ID.SelectedValue != "0")
            {
                int PSortId = ValueHandler.GetIntNumberValue(this.ddl_PInfo_PSort_ID.SelectedValue);
                ProductSortOp SortManager = new ProductSortOp();
                DataTable dtSortList = SortManager.GetSubProductSort(PSortId);
                string strSortIds = "PInfo_PSort_ID IN("+PSortId.ToString()+",";
                for (int i = 0; i < dtSortList.Rows.Count; i++)
                {
                    strSortIds += ValueHandler.GetIntNumberValue(dtSortList.Rows[i]["PSort_ID"]).ToString() + ",";
                }
                strCondition += strSortIds.Substring(0, strSortIds.Length - 1) + ") AND ";
            }
            if (tb_SDeta_PInfo_Id.Text.Trim() != "")
            {
                strCondition += " SDeta_PInfo_Id=" + this.hf_SDeta_PInfo_Id.Text + " AND ";
            }
            if (this.nb_SDeta_RealNumber1.Text.Trim() != "")
            {
                strCondition += " SDeta_RealNumber>=" + this.nb_SDeta_RealNumber1.Text + " AND ";
            }
            if (this.nb_SDeta_RealNumber2.Text.Trim() != "")
            {
                strCondition += "SDeta_RealNumber<="+this.nb_SDeta_RealNumber2.Text+" AND ";
            }
            if (!string.IsNullOrEmpty(strCondition))
                strCondition = strCondition.Substring(0, strCondition.Length - 4);

        }
 
        private void BindDropDownList()
        {

            ProductSortOp SortManager = new ProductSortOp();
            dtList = SortManager.GetList("");
            dtList.Columns.Add(new DataColumn("ilevel", typeof(System.Int32)));
            DataTable dtRes = dtList.Clone();
            DataRow dr = dtRes.NewRow();
            dr["PSort_ID"] = 0;
            dr["PSort_Code"] = "";
            dr["PSort_Name"] = "公司产品库";
            dr["PSort_PSort_ID"] = 0;
            dr["ilevel"] = 1;
            dtRes.Rows.Add(dr);
            ConstructLevel(dtRes, 0, 1);
            ddl_PInfo_PSort_ID.DataSource = dtRes;
            ddl_PInfo_PSort_ID.DataBind();
            //获取仓库列表
            DataTable dtStockList= Manager.GetStockList("");
            this.ddl_SDeta_Stock_Id.DataSource = dtStockList;
            this.ddl_SDeta_Stock_Id.DataBind();

            // ddl_PInfo_PSort_ID.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));            
        }
        private void ConstructLevel(DataTable dtRes, int id, int ilevel)
        {
            DataView dv = new DataView(dtList);
            dv.RowFilter = "PSort_PSort_ID=" + id.ToString();
            if (dv.Count > 0) ilevel++;
            for (int i = 0; i < dv.Count; i++)
            {
                dv[i]["ilevel"] = ilevel;
                dtRes.ImportRow(dv[i].Row);
                ConstructLevel(dtRes, ValueHandler.GetIntNumberValue(dv[i]["PSort_ID"]), ilevel);
            }
        }
        #endregion

        #region Property
        StockOp Manager = new StockOp();

        #endregion
    }
}
